package mail;

import java.util.Date;
import java.util.Properties;

import javax.activation.CommandMap;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.activation.MailcapCommandMap;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;


public class MailSender {
    private static final String MAIL_HOST="smtp.sina.cn";  //发件人邮箱的SMTP服务器
    private static final String MAIL_USERNAME="seightday";  //发件人邮箱登录用户名
    private static final String MAIL_PASSWORD="123456";  //发件人邮箱登录密码
    private static final String MAIL_FROM = "seightday@sina.cn";  //发件人邮箱地址
   

    //发送不包含附件邮件函数
    public static void sendMail(String to,String subject,String content) {
         Properties props = new Properties();
         props.put("mail.smtp.host", MAIL_HOST); //指定SMTP服务器
         props.put("mail.smtp.auth", "true"); //指定是否需要SMTP验证
        
         try {
              Session mailSession = Session.getDefaultInstance(props);
              mailSession.setDebug(true); //是否在控制台显示debug信息
             
              Message message=new MimeMessage(mailSession);
              message.setFrom(new InternetAddress(MAIL_FROM)); //发件人
              message.addRecipient(Message.RecipientType.TO,new InternetAddress(to)); //收件人
              message.setSentDate(new Date()); //发送日期
              message.setSubject(subject); //邮件主题
              //message.setText(content); //邮件内容
              message.setContent(content, "text/html;charset=gb2312"); //html格式邮件内容
              message.saveChanges();
             
              Transport transport = mailSession.getTransport("smtp");
              transport.connect(MAIL_HOST, MAIL_USERNAME, MAIL_PASSWORD);
              transport.sendMessage(message, message.getAllRecipients());
              transport.close();
          
         } catch (Exception e) {
              e.printStackTrace();
         }
     
    }
   

    //发送包含附件邮件函数
    public static void sendMailWithAttachment(String to,String subject,String content,String filePath){
         Properties props = new Properties();
         props.put("mail.smtp.host", MAIL_HOST); //指定SMTP服务器
         props.put("mail.smtp.auth", "true"); //指定是否需要SMTP验证
     
         try {
              Session mailSession = Session.getDefaultInstance(props);
              mailSession.setDebug(true); //是否在控制台显示debug信息
         
              MimeMessage message=new MimeMessage(mailSession);
              message.setFrom(new InternetAddress(MAIL_FROM)); //发件人
              message.addRecipient(Message.RecipientType.TO,new InternetAddress(to)); //收件人
              message.setSentDate(new Date()); //发送日期
              message.setSubject(subject); //邮件主题
         
              MimeBodyPart textBodyPart=new MimeBodyPart();
              //textBodyPart.setText(content);//邮件文本内容
              textBodyPart.setContent(content,"text/html;charset=gb2312"); //html格式邮件文本内容
         
              MimeBodyPart fileBodyPart=new MimeBodyPart();
              FileDataSource fds=new FileDataSource(filePath); //要发送的附件
              fileBodyPart.setDataHandler(new DataHandler(fds));
              fileBodyPart.setFileName(fds.getName());
         
              Multipart container=new MimeMultipart();
              container.addBodyPart(textBodyPart);
              container.addBodyPart(fileBodyPart);
              message.setContent(container); //邮件内容
        
              //不加下面这段就无法发送附件并抛出异常
              MailcapCommandMap mc = (MailcapCommandMap) CommandMap.getDefaultCommandMap();
              mc.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html");
              mc.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml");
              mc.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain");
              mc.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed");
              mc.addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822");
              CommandMap.setDefaultCommandMap(mc);
         
              Transport transport = mailSession.getTransport("smtp");
              transport.connect(MAIL_HOST, MAIL_USERNAME, MAIL_PASSWORD);
              transport.sendMessage(message, message.getAllRecipients());
              transport.close();
        
         } catch (Exception e) {
             e.printStackTrace();
         }
    }
   

    //测试函数
    public static void main(String[] args) {
          String to="seightday@gmail.com";
          String subject="javamail测试邮件";
          String content="<html> <body> <strong>邮件内容</strong> </body> </html>";
          MailSender.sendMail(to, subject, content);
//          MailSender.sendMailWithAttachment(to, subject, content,"D://library.zip");
    }

    /*
     如果出现java.lang.NoClassDefFoundError: com/sun/mail/util/...异常，
     是因为J2EE5里面的activation与mail包有冲突，把这两个包删掉，替换成J2EE1.4里面的activation.jar与mail.jar即可。
    */

}